{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "import matplotlib.patches as patches\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import os\n",
    "import random\n",
    "from scipy.ndimage.interpolation import zoom\n",
    "from scipy.misc import imsave, imshow\n",
    "import shutil"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def load_data():\n",
    "    \"\"\" Download MNIST data from TensorFlow package, \"\"\"\n",
    "    mnist = input_data.read_data_sets(\"MNIST_data\", one_hot=False)\n",
    "    train_data = mnist.train.images\n",
    "    test_data = mnist.test.images\n",
    "    valid_data = mnist.validation.images\n",
    "    train_label = mnist.train.labels\n",
    "    test_label = mnist.test.labels\n",
    "    valid_label = mnist.validation.labels\n",
    "    all_data = [valid_data, train_data, test_data]\n",
    "    all_labels = [valid_label, train_label, test_label]\n",
    "    return all_data, all_labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.\n",
      "Extracting MNIST_data/train-images-idx3-ubyte.gz\n",
      "Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.\n",
      "Extracting MNIST_data/train-labels-idx1-ubyte.gz\n",
      "Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.\n",
      "Extracting MNIST_data/t10k-images-idx3-ubyte.gz\n",
      "Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.\n",
      "Extracting MNIST_data/t10k-labels-idx1-ubyte.gz\n"
     ]
    }
   ],
   "source": [
    "all_data, all_labels = load_data()\n",
    "shutil.rmtree(\"MNIST_data\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAP8AAAD8CAYAAAC4nHJkAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAADlNJREFUeJzt3X+QVfV5x/HPE9gFQtiGlZasug3BwThoGmx3IIlMJw41\ngw4VbWeYkB+SjoU48UedpDMa7Q//6jCx0dK0YbpEInYSTNJoYRJTq2s6JBOKLoYICgrNkLAMAgYn\nYNOuu8vTP/aQrrj3e6/3nHvPXZ73a2Zn7z3POfc8c/TDufd+z56vubsAxPO2shsAUA7CDwRF+IGg\nCD8QFOEHgiL8QFCEHwiK8ANBEX4gqMnN3Fm7TfGpmt7MXQKh/K/+W6/7oNWybq7wm9lSSeskTZL0\nFXdfm1p/qqZrkS3Js0sACTu8r+Z1637bb2aTJP2jpKslzZe00szm1/t6AJorz2f+hZIOuPtP3f11\nSQ9LWl5MWwAaLU/4L5B0aMzzgWzZG5jZGjPrN7P+IQ3m2B2AIjX8235373X3HnfvadOURu8OQI3y\nhP+wpO4xzy/MlgGYAPKE/xlJ88zsPWbWLumjkrYW0xaARqt7qM/dh83sFkmPa3Sob6O7P19YZwAa\nKtc4v7s/JumxgnoB0ERc3gsERfiBoAg/EBThB4Ii/EBQhB8IivADQRF+ICjCDwRF+IGgCD8QFOEH\ngiL8QFCEHwiK8ANBEX4gKMIPBEX4gaAIPxAU4QeCIvxAUIQfCIrwA0ERfiAowg8ERfiBoAg/EBTh\nB4Ii/EBQuWbpNbODkk5JGpE07O49RTSF4pz83kXJ+rbf+Way/vSgJeurv3pLsj5r93DF2rR/fTq5\nLRorV/gzV7r7KwW8DoAm4m0/EFTe8LukJ81sp5mtKaIhAM2R923/Ync/bGa/JekJM9vn7tvGrpD9\no7BGkqbq7Tl3B6Aouc787n44+31M0qOSFo6zTq+797h7T5um5NkdgALVHX4zm25mM848lvQRSXuK\nagxAY+V52z9b0qNmduZ1vu7u/1ZIVwAazty9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CossPfW/L+U1q1t1btS6K3epXSW6mf+QGUp+wzP4CSlBJ+M1tqZi+a2QEzu7OMHioxs4NmttvM\ndplZf8m9bDSzY2a2Z8yyTjN7wsz2Z7/HnSatpN7uMbPD2bHbZWbXlNRbt5l938xeMLPnzezPsuWl\nHrtEX6Uct6a/7TezSZJeknSVpAFJz0ha6e4vNLWRCszsoKQedy99TNjMfl/Sa5IecvfLsmVfkHTC\n3ddm/3DOdPc7WqS3eyS9VvbMzdmEMl1jZ5aWdJ2kT6nEY5foa4VKOG5lnPkXSjrg7j9199clPSxp\neQl9tDx33ybpxFmLl0valD3epNH/eZquQm8twd2PuPuz2eNTks7MLF3qsUv0VYoywn+BpENjng+o\ntab8dklPmtlOM1tTdjPjmJ1Nmy5JL0uaXWYz46g6c3MznTWzdMscu3pmvC4aX/i92WJ3XyDpakk3\nZ29vW5KPfmZrpeGammZubpZxZpb+tTKPXb0zXhetjPAfltQ95vmF2bKW4O6Hs9/HJD2q1pt9+OiZ\nSVKz38dK7ufXWmnm5vFmllYLHLtWmvG6jPA/I2memb3HzNolfVTS1hL6eBMzm559ESMzmy7pI2q9\n2Ye3SlqVPV4laUuJvbxBq8zcXGlmaZV87Fpuxmt3b/qPpGs0+o3/f0m6u4weKvQ1V9JPsp/ny+5N\n0maNvg0c0uh3IzdKOk9Sn6T9kp6U1NlCvf2zpN2SntNo0LpK6m2xRt/SPydpV/ZzTdnHLtFXKceN\nK/yAoPjCDwiK8ANBEX4gKMIPBEX4gaAIPxAU4QeCIvxAUP8He3RTTCZVgjkAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f2290cc17b8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n"
     ]
    }
   ],
   "source": [
    "example_idx = random.randint(0,50000)\n",
    "data = all_data[1]\n",
    "labels = all_labels[1]\n",
    "\n",
    "image = data[example_idx].reshape((28,28))\n",
    "image_dim = 128\n",
    "label_np = labels[example_idx].astype(\"int32\")\n",
    "label = label_np.tolist()\n",
    "\n",
    "plt.imshow(image)\n",
    "plt.show()\n",
    "print(label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def generate_cluttered_digit(input_image, image_dim, label, data):\n",
    "    \"\"\"\n",
    "    :param input_image: input image to be stored\n",
    "    :param image_dim: int, side length of square image\n",
    "    :param data: entire dataset in memory from which we will pick fragments\n",
    "    :param label: single integer representing the digit in the cluttered image\n",
    "    :return: image_out: ndarray size image_dim x image_dim with digit randomly placed with\n",
    "    8 x 8 patches of images with input noise.\n",
    "    \"\"\"\n",
    "\n",
    "    # Initialize Blank image_out\n",
    "    image_out = np.zeros([image_dim, image_dim])\n",
    "\n",
    "    # Randomly Scale image\n",
    "    h = np.random.randint(low=int(28/2), high=int(28*2))\n",
    "    w = np.random.randint(low=int(28/2), high=int(28*2))\n",
    "    digit = zoom(input_image, (h/28, w/28))\n",
    "\n",
    "    # Randomly choose location in image_out\n",
    "    x = np.random.randint(low=0, high=image_dim - w)\n",
    "    y = np.random.randint(low=0, high=image_dim - h)\n",
    "\n",
    "    # Insert digit into image_out and get max\n",
    "    image_out[y:y + h, x:x + w] += digit\n",
    "    max_val = image_out.max()\n",
    "    \n",
    "    # Tighten box\n",
    "    rows = np.sum(image_out,axis=0).round(1)\n",
    "    cols = np.sum(image_out,axis=1).round(1)\n",
    "    \n",
    "    left = np.nonzero(rows)[0][0]\n",
    "    right = np.nonzero(rows)[0][-1]\n",
    "    upper = np.nonzero(cols)[0][0]\n",
    "    lower = np.nonzero(cols)[0][-1]\n",
    "    \n",
    "    # If box is too narrow or too short, pad it out to >12\n",
    "    width = right - left\n",
    "    if width < 12:\n",
    "        pad = np.ceil((12 - width)/2)\n",
    "        left  = int(left - pad)\n",
    "        right = int(right + pad)\n",
    "    \n",
    "    height = lower - upper\n",
    "    if height < 12:\n",
    "        pad = np.ceil((12 - height)/2)\n",
    "        upper = int(upper - pad)\n",
    "        lower = int(lower + pad)\n",
    "\n",
    "    # Save Ground Truth Bounding boxes with Label in 4th position\n",
    "    if label == 0:  # Faster RCNN regards 0 as background, so change the label for all zeros to 10\n",
    "        label = 10\n",
    "    gt_box = [left, upper, right, lower, label]\n",
    "    \n",
    "    # Track \"energy\" in gt_box (to prevent clutter insertion)\n",
    "    energy = np.sum(image_out[upper:lower, left:right])\n",
    "\n",
    "    # Add in total number of clutter patches\n",
    "    for j in range(int(image_dim/4)):\n",
    "\n",
    "        # Randomly select MNIST digit\n",
    "        index = np.random.choice(len(data))\n",
    "        fragment = np.reshape(data[index, :], (28, 28))\n",
    "\n",
    "        # Randomly select patch of selected digit\n",
    "        px = np.random.randint(low=0, high=28 - 8)\n",
    "        py = np.random.randint(low=0, high=28 - 8)\n",
    "\n",
    "        # Randomly choose location to insert clutter\n",
    "        x = np.random.randint(low=0, high=image_dim - 8)\n",
    "        y = np.random.randint(low=0, high=image_dim - 8)\n",
    "            \n",
    "        # Insert digit fragment  \n",
    "        image_out[y:(y+8), x:(x+8)] += fragment[py:(py+8), px:(px+8)]\n",
    "        \n",
    "        # Don't insert clutter into the true bounding box\n",
    "        new_energy = np.sum(image_out[upper:lower, left:right])\n",
    "        if energy != new_energy:\n",
    "            print(\"invading space \" + str(x) + \" \" + str(y))\n",
    "            image_out[y:(y+8), x:(x+8)] -= fragment[py:(py+8), px:(px+8)]\n",
    "            continue\n",
    "        \n",
    "    # Normalize any over-saturated pixels\n",
    "    image_out = np.clip(image_out, 0, max_val)\n",
    "        \n",
    "    # Subtract mean from image and scale to be between -1 and 1\n",
    "    image_out -= image_out.mean()\n",
    "    image_out = image_out / np.abs(image_out).max()\n",
    "\n",
    "    return image_out, gt_box"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "invading space 90 42\n",
      "invading space 99 60\n",
      "invading space 109 42\n",
      "invading space 100 68\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAQUAAAD8CAYAAAB+fLH0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xt8VNW1wPHfyuRFCAESJIR3kPCuoFIEtUoFbqkvbG8v\nV6sVlZb6qa3W9rZCe2+9bbW1VkVpqb3UF21pkYtWqPWB4uNaEQQEFOUVESQI4f0QSEhm1v1jnzwO\nSSBkMjNnkvX9fPhk5pwzc1ZIsmbtffbZW1QVY4ypkpLoAIwxwWJJwRjjY0nBGONjScEY42NJwRjj\nY0nBGONjScEY4xOzpCAi40Vkg4gUi8jUWJ3HGNO8JBaDl0QkBGwExgElwHLgGlX9oNlPZoxpVqkx\net8RQLGqbgYQkbnABKDepJAuGZpJ2xiFYowBOMz+Pap6xqmOi1VS6AZsq/W8BDiv9gEiMgWYApBJ\nFufJmBiFYowBeFnnb23McQnraFTVWao6XFWHp5GRqDCMMSeIVVLYDvSo9by7t80YE3CxSgrLgSIR\nKRSRdOBqYGGMzmWMaUYx6VNQ1UoR+TbwIhACHlPV92NxLmNM84pVRyOq+hzwXKze3xgTGzai0Rjj\nY0nBGONjScEY42NJwRjjY0nBGONjScEY42NJwRjjE7NxCqaulGGDAOjy+218KW+lb19IIoTVn6Nn\nFvWLW2zGVLGkEEea6v7oc9OOJDgSYxpmzQdjjI8lhTjSFWvRFWtZ8d/DEx2KMQ2ypGCM8bE+hQTI\n/PvbzPy760Tcdcv5AFRkQ9ZON19mx9lvNfs5Q4P7E35/Q7O/r2l5LCkkWOeZS+Jynk8uyWPq0/7Z\nuI5EMnj4/i+5OBYUAxDevTsu8ZjgsuaDMcanRVcK2+9wpXn+uBIAvtd7EWmEAaggBMCMvgMSE1yc\n3X3bY3W2tQsd46dTHwfg3tLrAcj8u1UKrZ1VCsYYnxZdKXT7lWuvp/5vbwB+PuoG/uunTwDUGT3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niYjhIeAFVR2AW9ZmXbzjEJFuwK3AcG/xoRBuLZF4xfEEddc5qffc\nMV7npL444rPeiqom9B8wCnix1vNpwLQExbIAGAdsAAq8bQXAhhiftzvul+0S4FlvW7xjaA98hNfP\nVGt7vOPoBmwDcnEzgz0L/Es84wB6A2tP9X9w4u8q8CIwKlZxnLDvS8CcWMSR8EqBml+CKglZK8Jb\n8OZsYBmQr6o7vF07gfwYn/5B3ES4kVrb4h1DIbAbeNxrxjwiIm3jHYeqbgfuAz4GdgAH1S0+FO//\nj9oaOncif3dvAp6PRRxBSAoJJyLZwFPAd1X1UO196lJvzC7RiEjVOp0rGzom1jF4UoFzgIdV9Wzc\nsHNfiR6POLz2+gRckuoKtBWR6+IdR0MSee4q0ay30hhBSAqNXisiFkQkDZcQ5qhq1TrypSJS4O0v\nAHbFMIQLgCtFZAswF7hERP4c5xjAfbqUqOoy7/l8XJKIdxxjgY9UdbeqVgBPA+cnII7aGjp33H93\na623cq2XoJo9jiAkheVAkYgUikg6rsNkYTxOLG5++keBdar6QK1dC4FJ3uNJuL6GmFDVaaraXVV7\n4773V1SaNNOnAAAAzklEQVT1unjG4MWxE9gmIv29TWNwU/XHNQ5cs2GkiGR5P58xuA7PeMdRW0Pn\njus6J3FbbyWWnUan0aFyKa439UPgx3E874W4UvBdYLX371IgD9fxtwl4GciNUzyjqelojHsMwDBg\nhff/8QzQMUFx/BRYD6wF/oRbYyQucQB/xfVlVOCqp8knOzfwY+/3dgPwxRjHUYzrO6j6Xf19LOKw\nEY3GGJ8gNB+MMQFiScEY42NJwRjjY0nBGONjScEY42NJwRjjY0nBGONjScEY4/P/EbOK1Etu+JcA\nAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f228e11f208>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[93, 44, 114, 78, 8]\n",
      "21\n"
     ]
    }
   ],
   "source": [
    "pixels, gt_box = generate_cluttered_digit(image, image_dim, label, data)\n",
    "fig, ax = plt.subplots(1)\n",
    "ax.imshow(pixels)\n",
    "height = gt_box[3] - gt_box[1]\n",
    "width = gt_box[2] - gt_box[0]\n",
    "rect = patches.Rectangle((gt_box[0], gt_box[1]), width, height, linewidth=2, edgecolor='r', facecolor='none')\n",
    "ax.add_patch(rect)\n",
    "plt.show()\n",
    "print(gt_box)\n",
    "print(gt_box[2]-gt_box[0])"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda env:tensorflow]",
   "language": "python",
   "name": "conda-env-tensorflow-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
